package color.koitq.picker;

import android.graphics.Typeface;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.QMUIViewPager;
import com.qmuiteam.qmui.widget.tab.QMUITab;
import com.qmuiteam.qmui.widget.tab.QMUITabBuilder;
import com.qmuiteam.qmui.widget.tab.QMUITabSegment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import color.koitq.picker.ad.AdActivity;
import color.koitq.picker.ad.AdConfig;
import color.koitq.picker.ad.AdManager;
import color.koitq.picker.base.BaseFragment;
import color.koitq.picker.fragment.HomeFrament;
import color.koitq.picker.fragment.SettingFragment;
import color.koitq.picker.fragment.Tab2Frament;
import color.koitq.picker.fragment.Tab3Frament;
import color.koitq.picker.fragment.Tab4Fragment;

public class MainActivity extends AdActivity {

    @BindView(R.id.pager)
    QMUIViewPager viewPager;
    @BindView(R.id.tabs)
    QMUITabSegment tabSegment;
    @BindView(R.id.bannerView)
    FrameLayout bannerView;

    private ArrayList<BaseFragment> mPages;
    private Map<String, Boolean> dialogMap = new HashMap<>();

    @Override
    protected int getContentViewId() {
        return R.layout.activity_main;
    }

    @Override
    protected void init() {
        overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
        initTabs();
        initPages();
        showDialogAd();
    }

    private void showDialogAd() {
        if (AdConfig.adDisable) {
            return;
        }
        if (AdConfig.vacation == AdConfig.VACATION_HOLIDAY) {
            AdManager.getInstance().setActivity(this).loadFullVideoAd(false);
        }
        showSecondPageAd_TwoBanner(bannerView, findViewById(R.id.bannerView2));
    }

    private void initTabs() {
        QMUITabBuilder builder = tabSegment.tabBuilder();
        builder.setTypeface(null, Typeface.DEFAULT_BOLD);
        builder.setSelectedIconScale(1.0f)
                .setTextSize(QMUIDisplayHelper.sp2px(this, 13), QMUIDisplayHelper.sp2px(this, 13))
                .setDynamicChangeIconColor(false);

        QMUITab homeTab = builder
                .setNormalDrawable(ContextCompat.getDrawable(this, R.mipmap.tab1_nor))
                .setSelectedDrawable(ContextCompat.getDrawable(this, R.mipmap.tab1_sel))
                .setDynamicChangeIconColor(false)
                .skinChangeWithTintColor(false)
                .build(this);

        QMUITab tab2 = builder
                .setNormalDrawable(ContextCompat.getDrawable(this, R.mipmap.tab2_nor))
                .setSelectedDrawable(ContextCompat.getDrawable(this, R.mipmap.tab2_sel))
                .setDynamicChangeIconColor(false)
                .skinChangeWithTintColor(false)
                .build(this);

        QMUITab tab3 = builder
                .setNormalDrawable(ContextCompat.getDrawable(this, R.mipmap.tab3_nor))
                .setSelectedDrawable(ContextCompat.getDrawable(this, R.mipmap.tab3_sel))
                .setDynamicChangeIconColor(false)
                .skinChangeWithTintColor(false)
                .build(this);

        QMUITab tab4 = builder
                .setNormalDrawable(ContextCompat.getDrawable(this, R.mipmap.tab4_nor))
                .setSelectedDrawable(ContextCompat.getDrawable(this, R.mipmap.tab4_sel))
                .setDynamicChangeIconColor(false)
                .skinChangeWithTintColor(false)
                .build(this);

        QMUITab setTab = builder
                .setNormalDrawable(ContextCompat.getDrawable(this, R.mipmap.tab5_nor))
                .setSelectedDrawable(ContextCompat.getDrawable(this, R.mipmap.tab5_sel))
                .setDynamicChangeIconColor(false)
                .skinChangeWithTintColor(false)
                .build(this);

        tabSegment.addTab(homeTab);
        tabSegment.addTab(tab2);
        tabSegment.addTab(tab3);
        tabSegment.addTab(tab4);
        tabSegment.addTab(setTab);
        tabSegment.notifyDataChanged();
    }

    private void initPages() {
        mPages = new ArrayList<>();
        mPages.add(new HomeFrament());
        mPages.add(new Tab2Frament());
        mPages.add(new Tab3Frament());
        mPages.add(new Tab4Fragment());
        mPages.add(new SettingFragment());

        viewPager.setAdapter(new PageAdapter(getSupportFragmentManager(), mPages));
        viewPager.setSwipeable(false);
        tabSegment.setupWithViewPager(viewPager, false);
    }

    private class PageAdapter extends FragmentPagerAdapter {

        private List<BaseFragment> list;

        public PageAdapter(@NonNull FragmentManager fm, List<BaseFragment> list) {
            super(fm);
            this.list = list;
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }
}